//
//  OrderViewController.m
//  LazyCat
//
//  Created by linhui on 15/12/11.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "OrderViewController.h"
#import "TabObject.h"
#import "SearchViewController.h"
#import "DetailOrderVC.h"
@interface OrderViewController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate,UIScrollViewDelegate>
{
    UIPageViewController *_pageViewCtrl;
    NSMutableArray *_dataArray;
    NSInteger _currentPage;
    UIView *_bgView;
    UIView *_lineView;
}
@end

@implementation OrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    [self configPageViewController];
}

-(void)backAction
{
    if ([self.tuisong isEqualToString:@"y"]) {
        [self dismissViewControllerAnimated:YES completion:^{
            
            
        }];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

- (void)createUI{
    self.view.backgroundColor = BackColor;
    NSString *title;
    if (_tuiHuoManager) {
        title = @"退货管理";
    }else {
        title = @"订单管理";
    }
    [self initNav:title doBack:YES];
    
    UIButton *backButton =[UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame=CGRectMake(8, 20+(44-21)/2, 30, 21);
//    backButton.backgroundColor = [UIColor redColor];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.navBar addSubview:backButton];
 
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 63, ScreenWidth, 1)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    lineView.alpha = 0.2;
    [self.navBar addSubview:lineView];
    //搜索按钮
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    searchBtn.frame = CGRectMake(ScreenWidth - 52, 30, 42, 30);
     
    [searchBtn addTarget:self action:@selector(searchBtn) forControlEvents:UIControlEventTouchUpInside];
    [searchBtn setImage:[UIImage imageNamed:@"searchLog"] forState:UIControlStateNormal];
    [searchBtn setImageEdgeInsets:UIEdgeInsetsMake(4, 10, 4, 10)];
    [self.navBar addSubview:searchBtn];
    _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, 40)];
    _bgView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:_bgView];
    _bgView.alpha = 1;
    UIView *buttomLine = [[UIView alloc]initWithFrame:CGRectMake(0, 104, ScreenWidth, 1)];
    buttomLine.backgroundColor = BackColor;
    buttomLine.alpha = 0.2;
    [self.view addSubview:buttomLine];
    _dataArray = [[NSMutableArray alloc]init];
    NSArray *array;
    if (_tuiHuoManager) {
        searchBtn.hidden = YES;
        array = @[@"申请退货",@"处理中",@"同意退货",@"已完成",@"所有订单"];
    }else {
        array = @[@"待发货",@"配送中",@"已完成",@"所有订单"];
    }
    for(int i=0;i<array.count;i++){
        TabObject *tab = [[TabObject alloc]init];
        tab.title = array[i];
        tab.subViewController = [[SubOrderViewController alloc]init];
        tab.subViewController.currentPage = i;
        tab.subViewController.tuiHuoManager = _tuiHuoManager;//传给子页面
        [_dataArray addObject:tab];
                __weak typeof(self)wSelf = self;
                [tab.subViewController setMyBlock:^(UIViewController *detailVC) {
                    if ([_tuisong isEqualToString:@"y"]) {
                        [wSelf presentViewController:detailVC animated:NO completion:nil];
                        DetailOrderVC *detail = (DetailOrderVC *)detailVC;
                        detail.tuisong = @"y";
                    }else {
                        [wSelf.navigationController pushViewController:detailVC animated:YES];
                    }
                }];
    }
    if (_todayOrder == 3) {
        _currentPage = 3;
    }else {
        _currentPage = 0;
    }
    _lineView = [[UIView alloc]init];
    _lineView.backgroundColor = SetColor(255,179, 14, 1);
    [_bgView addSubview:_lineView];
    for(int i=0;i<_dataArray.count;i++){
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i*(ScreenWidth/_dataArray.count), 0, ScreenWidth/_dataArray.count, 40);
        [btn setTitle:array[i] forState:UIControlStateNormal];
        [btn setTitleColor:SetColor(51, 51, 51, 1) forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitleColor:SetColor(255,179, 14, 1) forState:UIControlStateSelected];
        [btn setBackgroundColor:[UIColor clearColor]];
        [btn addTarget:self action:@selector(changeTab:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 100 + i;
        [_bgView addSubview:btn];
        if(i==_currentPage){
            btn.selected = YES;
            if (_tuiHuoManager) {
                _lineView.frame = CGRectMake(i*(ScreenWidth/5), 38, ScreenWidth/5, 2);
            }else {
                _lineView.frame = CGRectMake(i*(ScreenWidth/4), 38, ScreenWidth/4, 2);
            }
        }
    }
}
- (void)searchBtn {
    SearchViewController *search = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
}
- (void)configPageViewController{
    _pageViewCtrl = [[UIPageViewController alloc]initWithTransitionStyle:(UIPageViewControllerTransitionStyleScroll) navigationOrientation:(UIPageViewControllerNavigationOrientationHorizontal) options:nil];
    SubOrderViewController * sub = [_dataArray[_currentPage] subViewController];
    
    [_pageViewCtrl setViewControllers:@[sub] direction:(UIPageViewControllerNavigationDirectionForward) animated:YES completion:^(BOOL finished) {
        
    }];
    _pageViewCtrl.view.frame = CGRectMake(0, 40+65, ScreenWidth, ScreenHeight-65-40);
    [self.view addSubview:_pageViewCtrl.view];
    _pageViewCtrl.dataSource = self;
    _pageViewCtrl.delegate = self;
    //获取显示所有view的滚动视图
    UIScrollView *sv = _pageViewCtrl.view.subviews[0];
    sv.delegate = self;
    [_pageViewCtrl.view.subviews[0] setDelegate:self];
}
//单击按钮时切换视图
- (void)changeTab:(UIButton *)sender {
    NSInteger index = sender.tag-100;
    SubOrderViewController *sub = [_dataArray[index] subViewController];
    __weak typeof(_bgView)wBgView = _bgView;
    __weak typeof(_lineView)wLineView = _lineView;
    __block typeof (_tuiHuoManager)wTui = _tuiHuoManager;
    //显示翻页后要显示sub，翻页方向根据单击的按钮决定
    [_pageViewCtrl setViewControllers:@[sub] direction:_currentPage>index animated:YES completion:^(BOOL finished) {
        _currentPage = index;
        for(UIView *view in wBgView.subviews){
            if([view isKindOfClass:[UIButton class]]){
                UIButton *tb = (UIButton *)view;
                if(tb.tag == sender.tag){
                    tb.selected=YES;
                }else {
                    tb.selected = NO;
                }}}
        if (wTui) {
           wLineView.frame = CGRectMake(sender.frame.origin.x, 38, ScreenWidth/5, 2);
        }else {
            wLineView.frame = CGRectMake(sender.frame.origin.x, 38, ScreenWidth/4, 2);
        }
    }];
}
#pragma mark --
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    SubOrderViewController *sub = (SubOrderViewController *)viewController;
    NSInteger index = sub.currentPage;
    index++;
    if(index>=_dataArray.count){
        index = 0;
        return nil;
    }
    SubOrderViewController *rsvc = [_dataArray[index]subViewController];
    return rsvc;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    SubOrderViewController *sub = (SubOrderViewController *)viewController;
    NSInteger index = sub.currentPage;
    index--;
    if(index<0){
        index = _dataArray.count-1;
        return nil;
    }
    SubOrderViewController *rsvc = [_dataArray[index]subViewController];
    return rsvc;
    
}
//翻页成功后执行的方法
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed{
    SubOrderViewController *sub = (SubOrderViewController *)pageViewController.viewControllers[0];
    _currentPage = sub.currentPage;
    for(UIView *view in _bgView.subviews){
        if([view isKindOfClass:[UIButton class]]){
            UIButton *btn = (UIButton *)view;
            if(btn.tag-100==_currentPage){
                btn.selected =YES;
            }else {
                btn.selected = NO;
            }
        }
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat offSetX = scrollView.contentOffset.x;
    if (_tuiHuoManager) {
        CGFloat ratio = ScreenWidth/5/ScreenWidth;
        CGRect frame = _lineView.frame;
        frame.origin.x = (offSetX-ScreenWidth)*ratio+_currentPage*ScreenWidth/5;
        _lineView.frame = frame;
    }else {
        CGFloat ratio = ScreenWidth/4/ScreenWidth;
        CGRect frame = _lineView.frame;
        frame.origin.x = (offSetX-ScreenWidth)*ratio+_currentPage*ScreenWidth/4;
        _lineView.frame = frame;
    }
    
    
}

@end
